﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Entity;
using BAL;
using Common;

namespace Web
{
    public partial class SelectKnow : System.Web.UI.Page
    {
        private int _SubjectId = 0, _BookId = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "QuesAdd"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }
            if (Request.QueryString["SubjectId"] != null)
            {
                _SubjectId = Convert.ToInt32(Request.QueryString["SubjectId"].Trim());
            }
            if (Request.QueryString["BookId"] != null)
            {
                _BookId = Convert.ToInt32(Request.QueryString["BookId"].Trim());
            }
            if (!IsPostBack)
            {
                LoadSubject(0);
                LoadKnow(0, _SubjectId, _BookId, "");
            }
        }

        protected void LoadSubject(int SubjectId)
        {
            //KnowList.Items.Clear();
            //if (_SubjectId == 0)
            //{
            //    IList<SubjectInfo> subjectInfos = Subject.GetSubjectInfos();
            //    foreach (SubjectInfo info in subjectInfos)
            //    {
            //        KnowList.Items.Add(new ListItem(info.Name, info.Keyword));
            //        LoadBook(info.Id);
            //    }
            //}
            TypeList.Items.Clear();
            TypeList.Items.Add(new ListItem("请选择类别", "0"));
            IList<SubjectInfo> subjectInfos = Subject.GetSubjectInfos();
            foreach (SubjectInfo info in subjectInfos)
            {
                TypeList.Items.Add(new ListItem(info.Name, "s" + info.Id.ToString()));
                LoadBook(info.Id);
            }
        }

        protected void LoadBook(int SubjectId)
        {
            //IList<BookInfo> bookInfos = Book.GetBookInfosBySubject(SubjectId, 0, 0);
            //foreach (BookInfo info in bookInfos)
            //{
            //    KnowList.Items.Add(new ListItem("——" + info.Name, info.Keyword));
            //    LoadKnow(0, info.Id, "————");
            //}

            IList<BookInfo> bookInfos = Book.GetBookInfosBySubject(SubjectId, 0, 0);
            foreach (BookInfo info in bookInfos)
            {
                TypeList.Items.Add(new ListItem("——|—" + info.Name, "b" + info.Id.ToString()));
            }
        }

        protected void LoadKnow(int ParentID, int SubjectId,int BookId, string line)
        {
            if (ParentID == 0)
            {
                IList<KnowledgeInfo> knowInfos = new List<KnowledgeInfo>();
                if (BookId > 0)
                {
                    knowInfos = Knowledge.GetKnowledgeInfos(BookId, 0);
                }
                else if (SubjectId > 0)
                {
                    knowInfos = Knowledge.GetKnowledgeInfos(0, SubjectId);
                }
                else
                {
                    knowInfos = Knowledge.GetKnowledgeInfos();
                }
                
                foreach (KnowledgeInfo info in knowInfos)
                {
                    ListItem item = new ListItem(line + "" + info.Name, info.Keyword);
                    KnowList.Items.Add(item);
                    LoadKnow(info.Id, SubjectId, BookId, " ——");
                }
            }
            else
            {
                IList<KnowledgeInfo> knowInfos = Knowledge.GetKnowledgeInfos(ParentID);
                foreach (KnowledgeInfo info in knowInfos)
                {
                    ListItem item = new ListItem(line + "|—" + info.Name, info.Keyword);
                    KnowList.Items.Add(item);
                    LoadKnow(info.Id,SubjectId, BookId, "——" + line);
                }
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Keyword.Text.Trim() != "")
            {
                IList<KnowledgeInfo> knowInfos = Knowledge.GetKnowledgeInfosBySearch(Keyword.Text.Trim());
                KnowList.Items.Clear();
                foreach (KnowledgeInfo info in knowInfos)
                {
                    ListItem item = new ListItem(info.Name, info.Keyword);
                    KnowList.Items.Add(item);
                }
            }
            else
            {
                Utility.ShowJsAlert("请输入关键字", this);
            }
        }



        protected void Button3_Click1(object sender, EventArgs e)
        {
            if (KnowList.SelectedValue == "")
            {
                Utility.ShowJsAlert("请选择知识点" + KnowList.SelectedValue, this);
            }
            else
            {

                if (!KnowList.SelectedValue.StartsWith("knowto"))
                {
                    Utility.ShowJsAlert("请选择一个知识点，而不是科目或者教材版本", this);
                }
                else
                {
                    String strJScript = "$('#KnowId', parent.document).val($('#KnowList').val());";
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "goddd", "<script>" + strJScript + "</script>");
                    Utility.ShowJs(strJScript, this);
                }
            }
        }

        protected void TypeList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (TypeList.SelectedValue != "0" && TypeList.SelectedValue != "")
            {
                if (TypeList.SelectedValue.StartsWith("b"))
                {
                    Response.Redirect("SelectKnow.aspx?bookid=" + TypeList.SelectedValue.Replace("b",""));
                }
                else if (TypeList.SelectedValue.StartsWith("s"))
                {
                    Response.Redirect("SelectKnow.aspx?subjectid=" + TypeList.SelectedValue.Replace("s",""));
                }
            }
        }
    }
}
